package eu.liquify.ui {
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BevelFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	public class ColorSwatch extends UIItem {
		public static const COLOR_UPDATE:String = "colorUpdate";
		
		private var _color:uint = 0xFF0000;
		
		private var colorDisplay:Sprite = new Sprite();
		private var swatchWidth:Number = 15;
		
		public var padding:Number = 2;
		
		private var prevColor:uint;
		
		protected var input:TextInput;
		
		private static const prefixTrim:RegExp = /\s*(?:0x|#)?(\S*)\s*/;
		private static const invalidStrip:RegExp = /[^0-9a-fA-F]/g;
		
		public function ColorSwatch(options:Object = null) {
			//addChild(gloss);
			
			input = new TextInput();
			input.centered = true;
			input.addEventListener(Event.CHANGE, inputChanged);
			input.addEventListener(TextInput.TEXT_UPDATE, inputValidate);
			addChild(input);
			
			colorDisplay.buttonMode = true;
			colorDisplay.addEventListener(MouseEvent.CLICK, dispatchOpen);
			addChild(colorDisplay);
			
			super();
			height = 20;
			applyOptions(options);
			updateColor();
			prevColor = color;
		}
		
		private function dispatchOpen(e:MouseEvent):void {
			dispatchEvent(new Event(Event.OPEN));
		}
		
		private function inputChanged(e:Event = null):void {
			input.text = input.text.replace(prefixTrim, "$1");
			var value:Number = getInputColor();
			redraw(value);
		}
		private function inputValidate(e:Event):void {
			var value:Number = getInputColor();
			if (!(isNaN(value) || value < 0 || value > 0xFFFFFF)) {
				color = value;
			}
			updateColor();
			dispatchEvent(new Event(COLOR_UPDATE));
		}
		public function getInputColor():Number {
			var str:String = input.text;
			str = str.replace(prefixTrim, "$1");
			str = str.replace(invalidStrip, "");
			str = str.substr(0, 6);
			return parseInt(str, 16);
		}
		
		public function get color():uint {
			return _color;
		}
		
		public function set color(value:uint):void {
			_color = value;
			updateColor();
		}
		
		private function updateColor():void {
			input.text = Utils.pad(color.toString(16), "0", 6).toUpperCase();
			redraw(color);
		}
		
		override public function resize():void {
			super.resize();
			
			input.width = width-swatchWidth;
			input.height = height;
			input.resize();
			input.x = swatchWidth;
			height = input.height;
			
			redraw(color);
		}
		
		private function redraw(fillColor:Number):void {
			var invalid:Boolean = isNaN(fillColor) || fillColor < 0 || fillColor > 0xFFFFFF;
			if (invalid) fillColor = 0xF3F3F3;
			
			var g:Graphics;
			g = colorDisplay.graphics;
			g.clear();
			g.lineStyle(0, 0xDBDBDB, 1, true);
			g.beginFill(fillColor);
			g.drawRect(0, 0, swatchWidth, height);
			g.endFill();
			if (invalid) {
				g.lineStyle(3, 0xE81111);
				g.moveTo(swatchWidth*0.3, height*0.3);
				g.lineTo(swatchWidth*0.7, height*0.7);
				g.moveTo(swatchWidth*0.7, height*0.3);
				g.lineTo(swatchWidth*0.3, height*0.7);
			}
		}
		
	}

}